import 'package:async/async.dart';
import 'package:boss/models/store_menu.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/provider/offers_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/planOffers/alert/showDiscountAlert.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/planOffers/model/plan_menu_discount.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/planOffers/provider/plan_offers_provider.dart';
import 'package:boss/ui/page/offersManagement/page/plusOffersMode/page/widget/offers_form_field_common.dart';
import 'package:boss/ui/widget/button/fixed_bottom_btn.dart';
import 'package:boss/ui/widget/custom_divier.dart';
import 'package:boss/ui/widget/custom_list_title.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/loader/loader.dart';

import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';

class PlanOffersBrowser extends StatefulWidget {
  @override
  _PlanOffersBrowserState createState() => _PlanOffersBrowserState();
}

class _PlanOffersBrowserState extends State<PlanOffersBrowser> {
  PlanOffersProvider planOffersProvider;

  Future<Result<List<StoreMenu>>> _loadTask() async {
    var commodityProvider = Provider.of<CommodityProvider>(
      context,
      listen: false,
    );
    var data = await commodityProvider.initData();

    if (data.isValue) {
      planOffersProvider = Provider.of<PlanOffersProvider>(
        context,
        listen: false,
      );
      planOffersProvider.setMenus(data.asValue.value
          .map((item) => PlanMenuDiscount(
                menu: item,
                controller: TextEditingController(),
              ))
          .toList());
    }

    return data;
  }

  @override
  void dispose() {
    planOffersProvider.menus.forEach((item) {
      item.controller.dispose();
      item.controller = null;
    });
    planOffersProvider = null;
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        Expanded(
          child: SingleChildScrollView(
            padding: EdgeInsets.symmetric(
              vertical: 30.w,
            ),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                OffersFormFieldCommon<PlanOffersProvider>(
                  title: '方案优惠',
                ),
                CustomDivider(
                  height: 20.w,
                ),
                Loader(
                  loadTask: _loadTask,
                  builder: (context) {
                    return Padding(
                      padding: EdgeInsets.symmetric(
                        horizontal: 55.w,
                      ),
                      child: Column(
                        children: <Widget>[
                          CustomListTitle(
                            title: '分类折扣率',
                            trailing: LabelButton(
                              title: '批量设置',
                              textStyle: TextStyleConstant().blue_28,
                              onTap: () async {
                                var result = await showDiscountAlert(context);
                                if (result != null) {
                                  var planOffersProvider =
                                      Provider.of<PlanOffersProvider>(
                                    context,
                                    listen: false,
                                  );
                                  planOffersProvider
                                      .setMenusDiscountRate(result);
                                }
                              },
                            ),
                            subTitle: '批量设置所有分类统一折扣率',
                          ),
                          Consumer<PlanOffersProvider>(
                            builder: (context, planOffersProvider, _) {
                              return ListView.builder(
                                shrinkWrap: true,
                                physics: NeverScrollableScrollPhysics(),
                                itemBuilder: (context, index) {
                                  var current = planOffersProvider.menus[index];

                                  return BaseTextFormField(
                                    controller: current.controller,
                                    prefixText: current.menu.menuName,
                                    hintText: '例：打九折，填写90',
                                    suffixText: '%',
                                    inputFormatters: [
                                      FilteringTextInputFormatter.allow(
                                          RegUtil.integerReg)
                                    ],
                                    keyboardType: TextInputType.number,
                                    maxLength: 2,
                                    onChanged: (value) {
                                      current.discountRate = value;
                                    },
                                  );
                                },
                                itemCount: planOffersProvider.menus.length,
                              );
                            },
                          ),
                        ],
                      ),
                    );
                  },
                ),
              ],
            ),
          ),
        ),
        FixedBottomBtn(
          title: '保 存',
          onTap: () async {
            var result = await planOffersProvider.addPlanOffers();
            if (result) {
              var offersProvider = Provider.of<OffersProvider>(
                context,
                listen: false,
              );
              offersProvider.backstageRefresh();
              Navigator.pop(context);
            }
          },
        ),
      ],
    );
  }
}
